package cn.yoyo.controller.adm;

import cn.yoyo.entity.SysUser;
import cn.yoyo.service.UserService;
import cn.yoyo.tools.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class AdmUserController {
    @Autowired
    UserService userService;

    public AdmUserController() {
        System.out.println("我是user");
    }

    @RequestMapping("/api/adm/user/findAll")
    public Map<String, Object> findAll(Integer start, Integer num) {
        Map<String, Object> map = new HashMap<>();
        map.put("total", userService.getTotal());
        map.put("users", userService.findAll(start, num));
        return RespJson.pack(200, "", map);
    }

    @RequestMapping("/api/adm/user/closed")
    public Map<String, Object> freeze(Integer id, Integer closed, Integer start, Integer num) {
        // id不能为空也不能小于等于0,closed不能为0和1以外的数
        if (id == null || closed == null
                && id <= 0 || (closed != 0 && closed != 1)) {
            return RespJson.pack(400, "冻结失败,传值有误", null);
        }
        userService.freeze(closed, id);// 冻结或解冻成功后 返回所有数据刷新前端页面
        if (closed == 0) {
            return RespJson.pack(200, "解冻成功", userService.findAll(start, num));
        }
        return RespJson.pack(200, "冻结成功", userService.findAll(start, num));
    }

    @RequestMapping("/api/adm/user/resetPwd/{uid}")
    public Map<String, Object> resetPassword(@PathVariable("uid") Integer uid) {
        System.out.println(uid);
        return RespJson.pack(200, "重置成功", userService.resetPassword(uid));
    }

    @RequestMapping("/api/adm/user/findById/{id}")
    public Map<String, Object> findById(@PathVariable("id") Integer id) {
        return RespJson.pack(200, "", userService.findById(id));
    }

    @RequestMapping("/api/adm/user/addAdm")
    public Map<String, Object> resetPassword(SysUser user) {
        return RespJson.pack(200, "添加成功", userService.addAdm(user));
    }
}
